package qlttn.web.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;


import qlttn.business.QuestionBO;
import qlttn.business.QuestionBOImpl;
import qlttn.model.Course;
import qlttn.model.GroupQuestion;
import qlttn.model.Images;
import qlttn.model.Question;
import qlttn.model.Users;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
/**
 * @author Anh Vanh
 *
 */
@ManagedBean
@SessionScoped
public class CreateQuestionBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String questionContent;
	private int numberOfAnswer;
	private String level;
	private String result;
	private String choice;
	private List<String> listContentChoice = new ArrayList<String>();
	private List<String> listAnswer = new ArrayList<String>();
	private Question question;
	private String questionGroup;
	private String course;
	private String date = "Create date";
	private QuestionBO questionBO;
	private UploadedFile uploadedFile;
	private static String imgPath;
	private String kindOf;
	private String isMultiChoice;
	private int kindIsMultiChoice;
	/**
	 * Define constructor empty for bean class.
	 */
	public CreateQuestionBean(){
		System.out.println("abc");
	}
	/**
	 * 
	 * @param numberOfAnswers
	 * @param level
	 * @param questionContent
	 */
	public CreateQuestionBean(int numberOfAnswer, String level,
			String questionContent) {
		this.numberOfAnswer = numberOfAnswer;
		this.level = level;
		this.questionContent = questionContent;
	}

	/**
	 * @return the numberOfAnswers
	 */
	public int getNumberOfAnswer() {
		return numberOfAnswer;
	}
	/**
	 * @param numberOfAnswers the numberOfAnswers to set
	 */
	public void setNumberOfAnswer(int numberOfAnswer) {
		this.numberOfAnswer = numberOfAnswer;
	}
	/**
	 * @return the level
	 */
	public String getLevel() {
		return level;
	}
	/**
	 * @param level the level to set
	 */
	public void setLevel(String level) {
		this.level = level;
		System.out.println(level);
	}
	/**
	 * @return the questionContent
	 */
	public String getQuestionContent() {
		return questionContent;
	}
	/**
	 * @param questionContent the questionContent to set
	 */
	public void setQuestionContent(String questionContent) {
		this.questionContent = questionContent;
		System.out.println(questionContent);
	}
	
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
		listAnswer.add(result);
	}
	
	
	
	public String getChoice() {
		return choice;
	}
	
	public String getQuestionGroup() {
		return questionGroup;
	}
	public void setQuestionGroup(String questionGroup) {
		this.questionGroup = questionGroup;
	}
	
	public String getCourse() {
		return course;
	}
	public void setCourse(String course) {
		this.course = course;
	}
	public void setChoice(String choice) {
		this.choice = choice;
		listContentChoice.add(choice);
		
		for(String s:listContentChoice)
			System.out.println(s);
	}
	
	
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	
	
	/**
	 * @return the kindOf
	 */
	public String getKindOf() {
		return kindOf;
	}
	/**
	 * @param kindOf the kindOf to set
	 */
	public void setKindOf(String kindOf) {
		this.kindOf = kindOf;
	}
	
	
	
	
	/**
	 * @return the isMultiChoice
	 */
	public String getIsMultiChoice() {
		return isMultiChoice;
	}
	/**
	 * @param isMultiChoice the isMultiChoice to set
	 */
	public void setIsMultiChoice(String isMultiChoice) {
		this.isMultiChoice = isMultiChoice;
	}
	public UploadedFile getUploadedFile() {
    	if(uploadedFile !=null)
    	System.out.println(uploadedFile.getName());
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
        if(uploadedFile !=null)
        System.out.println(uploadedFile.getName());
    }


	public String getImgPath() {
		return imgPath;
	}


	public void setImgPath(String imgPaths) {
		imgPath = imgPaths;
	}
	
	public static String getPathImg(){
		return imgPath;
	}
	
	
	/**
	 * @return the kindIsMultiChoice
	 */
	public int getKindIsMultiChoice() {
		return kindIsMultiChoice;
	}
	/**
	 * @param kindIsMultiChoice the kindIsMultiChoice to set
	 */
	public void setKindIsMultiChoice(int kindIsMultiChoice) {
		this.kindIsMultiChoice = kindIsMultiChoice;
	}
	/**
	 * 
	 * @return
	 */
	public List<SelectItem> getKindOfQuestion() {
		List<SelectItem> numberOfChoice = new ArrayList<SelectItem>();
		numberOfChoice.add(new SelectItem("0", "---Kind Of Question---"));
		numberOfChoice.add(new SelectItem("MultiChoice", "MultiChoice"));
		numberOfChoice.add(new SelectItem("TrueFalseQuestion", "TrueFalseQuestion"));
		numberOfChoice.add(new SelectItem("FillText", "FillText"));
		return (numberOfChoice);
	}
	
	
	public List<SelectItem> getNumberOfChoice() {
		List<SelectItem> numberOfChoice = new ArrayList<SelectItem>();
		numberOfChoice.add(new SelectItem("0", "---Number of Choice---"));
		numberOfChoice.add(new SelectItem("2", "2"));
		numberOfChoice.add(new SelectItem("3", "3"));
		numberOfChoice.add(new SelectItem("4", "4"));
		numberOfChoice.add(new SelectItem("5", "5"));
		numberOfChoice.add(new SelectItem("6", "6"));
		return (numberOfChoice);
	}
	

	public SelectItem[] getListLevel() {
		SelectItem[] listLevel = { new SelectItem("unknow", "---Choice level---"),
				new SelectItem("Easy", "Easy"),
				new SelectItem("Midium", "Midium"),
				new SelectItem("Hard", "Hard") };
		
		return listLevel;
	}
	
	public List<String> getListAnswer(){
		List<String> listAnswer = new ArrayList<String>();
		listAnswer.add("A");
		listAnswer.add("B");
		listAnswer.add("C");
		listAnswer.add("D");
		listAnswer.add("E");
		listAnswer.add("F");
		return listAnswer;
	}
	
	public List<SelectItem> getListQuestionGroup(){
		List<SelectItem> listQuestionGroup = new ArrayList<SelectItem>();
		listQuestionGroup.add(new SelectItem("0", "---Group Question---"));
		listQuestionGroup.add(new SelectItem("1", "Đại Số"));
		listQuestionGroup.add(new SelectItem("1","Hình Học"));
		listQuestionGroup.add(new SelectItem("2","Cơ học"));
		listQuestionGroup.add(new SelectItem("2","Động Lực "));
		listQuestionGroup.add(new SelectItem("3","Hữu Cơ"));
		listQuestionGroup.add(new SelectItem("3","Vô Cơ"));
		return (listQuestionGroup);
	}
	
	public List<SelectItem> getListCourse(){
		List<SelectItem> listCourse = new ArrayList<SelectItem>();
		listCourse.add(new SelectItem("0", "---Course---"));
		listCourse.add(new SelectItem("1", "Toán"));
		listCourse.add(new SelectItem("2","Lý"));
		listCourse.add(new SelectItem("3","Hóa"));
		listCourse.add(new SelectItem("4","Anh Văn"));
		
		return (listCourse);
	}
	
	
	private Date convertToDate(String strDate){
		Date date = new Date();
		StringTokenizer token = new StringTokenizer(strDate, "/");
		int day, month, year;
		while(token.hasMoreTokens()){
			month = Integer.valueOf(token.nextToken());
			day = Integer.valueOf(token.nextToken());
			year = Integer.valueOf(token.nextToken());
			date.setDate(day);
			date.setMonth(month);
			date.setYear(year);
		}
		return date;
	}
	public String loadFile() {
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    	String path = request.getServletContext().getRealPath("/images");
    	
//    	String path = "D:/tai lieu fpt/tai lieu/update project/QLTTN/WebContent/images";
    	System.out.println(uploadedFile);
        String fileName = FilenameUtils.getName(uploadedFile.getName());
        imgPath = fileName.trim();
        File file = new File(path+"/"+imgPath);
        if(!file.exists()){
        	try {
    			byte[] bytes = uploadedFile.getBytes();
    			 
    			 System.out.println(imgPath+"...Ok");
    		        FileOutputStream fos = new FileOutputStream(path+"/"+imgPath);
    		        fos.write(bytes);
    		        fos.flush();
    		        fos.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        }
        return null;
    }
    

    
	
	public void checkKindOfQuestion(ValueChangeEvent event){
		if(event.getNewValue().equals("MultiChoice")){
			this.kindIsMultiChoice = 1;
		}else{
			this.kindIsMultiChoice = 0;
		}
		System.out.println("kindIsMultiChoice :"+kindIsMultiChoice);
	}
	
	public String saveQuestion(){
		questionBO = new QuestionBOImpl();
		String forward = "createExamQuestion";
		System.out.println(questionGroup);
		int groupID = Integer.parseInt(questionGroup);
		int courseID = Integer.parseInt(course);
		Course course = new Course(courseID);
		GroupQuestion groupQuestion = new GroupQuestion(groupID, course);
		Users user = new Users(2);
		Date d = this.convertToDate(date);
		
		String pathImg = UploadImageBean.getPathImg();
		Set<Images> setImg = new HashSet<Images>();
		setImg.add(new Images(pathImg));
		
		
		question = new Question(groupQuestion, user, questionContent, d, null,level, kindOf);
		boolean result = questionBO.save(question);
		System.out.println(result);
		System.out.println("Level"+level);
		System.out.println("Course"+courseID);
		System.out.println("Group"+groupID);
		System.out.println("Date"+date);
		System.out.println("Qcontent"+questionContent);
		System.out.println("List choice"+listContentChoice.get(0)+" "+listContentChoice.get(1));
		System.out.println("Path Image "+pathImg);
		
		return forward;
	}
	
}
